package com.kfm.am.exec;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Exec02 {

    public static void main(String[] args) {
        // 将大文件拆分成多个小文件，每个小文件的大小可以自定义，这样可以方便传输，也可以方便存储。
        String target = "E:\\home\\sp\\game\\syyj.mp4";
        String dest = "E:\\home\\sp\\cutter";
//        split(target, dest, Integer.MAX_VALUE);
//        String target2 = "E:\\home\\sp\\game\\syyj2.mp4";
//        join(dest, target2);
    }

    private static void split(String target, String dest, long size) {
        File targetFile = new File(target);
        File destFile = new File(dest);
        if (!targetFile.exists()) {
            throw new RuntimeException("要切分的文件不存在！");
        }
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        // 加载要切分的数据源
        try (FileInputStream in = new FileInputStream(targetFile);) {
            int count = 0;
            byte[] bytes = new byte[(int) size];
            while (in.available() > 0) {
                int readSize = in.read(bytes);
                // 构建要写出去的文件名字
                String name = ++count + ".tmp";
                try (FileOutputStream out = new FileOutputStream(new File(destFile, name));) {
                    out.write(bytes, 0, readSize);
                }
            }
            System.out.println("文件分割完毕，分成" + count + "个文件");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void split2(String target, String dest, long size) {
        File targetFile = new File(target);
        File destFile = new File(dest);
        if (!targetFile.exists()) {
            throw new RuntimeException("要切分的文件不存在！");
        }
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        // 加载要切分的数据源
        try (FileInputStream in = new FileInputStream(targetFile);) {
            int count = 0;
            while (in.available() > 0) { // 结束条件 流里面没有东西了
                // byte[] bytes = new byte[(int)size];
                if (size > Integer.MAX_VALUE - 8) { // 数组的最大安全长度是 Integer.MAX_VALUE - 8
                    long sum = size;
                    String name = ++count + ".tmp";
                    try(FileOutputStream out = new FileOutputStream(new File(destFile, name), true);) {
                        while (sum > Integer.MAX_VALUE - 8 && in.available() > 0) {
                            byte[] bytes = new byte[Integer.MAX_VALUE - 8];
                            int readSize = in.read(bytes);
                            out.write(bytes, 0, readSize);
                            sum -= readSize;
                        }
                        if (in.available() > 0) { // 一次的size位还没读完
                            byte[] bytes = new byte[(int) sum];
                            int readSize = in.read(bytes);
                            out.write(bytes, 0, readSize);
                        }  // 从输入流in里面读取 size长度的字节到输出流 out
                    }
                } else {
                    byte[] bytes = new byte[(int) size];
                    String name = ++count + ".tmp";
                    try (FileOutputStream out = new FileOutputStream(new File(destFile, name), true);) {
                        int readSize = in.read(bytes);
                        out.write(bytes, 0, readSize);
                    } // 从in 输入流读取size个字节 放到out 输出流中
                }
            }
            System.out.println("文件分割完毕，分成" + count + "个文件");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    private static void join(String target, String dest) {
        // 将多个小文件合并成一个大文件，这样可以方便存储，也可以方便传输。
        File targetFile = new File(target);
        File destFile = new File(dest);
        if (!targetFile.exists()) {
            throw new RuntimeException("小文件所在的文件夹不存在");
        }
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        File[] files = targetFile.listFiles(f -> f.isFile() && f.getName().endsWith(".tmp"));
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                int i1 = Integer.parseInt(o1.getName().split("\\.")[0]); // "1" "tmp"
                int i2 = Integer.parseInt(o2.getName().split("\\.")[0]);
                // o1.getName().substring(0, o1.getName().lastIndexOf('.'));
                return i1 - i2;
            }
        });
        // 1.tmp 2.tmp  3.tmp
        try (FileOutputStream out = new FileOutputStream(destFile, true);) {
            for (File file : files) {
                try (FileInputStream in = new FileInputStream(file);) {
                    byte[] bytes = in.readAllBytes();
                    out.write(bytes);
                }
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
